<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once('Gecko/Form/Renderer/Interface.php');

/**
 * Template form renderer, builds the form into
 * a Gecko_Template template
 *
 * @see Gecko_Template
 * @package Gecko.Form.Renderer;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Renderer_Template implements Gecko_Form_Renderer_Interface {
	/**
	 * The form Template
	 *
	 * @var string
	 */
	private $template;

	/**
	 * The form start code
	 *
	 * @var string
	 */
	private $start = '';

	/**
	 * The form end code
	 *
	 * @var string
	 */
	private $end = '';

	/**
	 * The form HTML code
	 *
	 * @var string
	 */
	private $html = '';

	/**
	 * The form items
	 *
	 * @var array
	 */
	private $items = array();

	/**
	 * The template vars
	 */
	private $vars = array();

	/**
	 * To separate the errors or have them in control
	 *
	 * @var boolean
	 */
	private $separateErrors = false;

	/**
	 * All of the errors if separating
	 *
	 * @var array
	 */
	private $errors = array();

	/**
	 * Creates a new renderer from the passed in template
	 *
	 * @param string The template to render
	 */
	public function __construct($template, $separateErrors = false) {
		$this->template = $template;
		$this->separateErrors = $separateErrors;
	}

	/**
	 * To separate the errors or not
	 *
	 * @param boolean $bool_separate To separate errors
	 */
	public function setSeparateErrors($bool_separate) {
		$this->separateErrors = (bool) $bool_separate;
	}

	/**
	 * Starts drawing the form
	 *
	 * @param string The form name
	 * @param string Form url
	 * @param string Form Method
	 * @param string The Form encoding type
	 */
	public function beginForm($name, $url, $method, $enctype) {
		$this->start = "<form name=\"$name\" action=\"$url\" id=\"$name\" method=\"$method\" enctype=\"$enctype\">";
	}

	/**
	 * Ends the form
	 */
	public function endForm() {
		$this->end = "</form>";
	}

	/**
	 * Adds a new Item into the table
	 *
	 * @param string The form name
	 * @param string The form html code
	 * @param array The field error messages
	 */
	public function addItem( $name, $html, $messages = null ) {
		if( $messages ) {
			if( $this->separateErrors ) {
				$this->errors[$name] = $messages;
			} else {
				if(is_array($messages)) {
					$messages = implode("<br />", $messages);
				}
				$html = "<span class=\"field-error\">$html $messages</span>";
			}
		}

		$this->items[$name] = $html;
	}

	/**
	 * Adds HTML code to the form
	 *
	 * @param string The html code
	 */
	public function addHTML( $html ) {
		$this->html .= $html;
	}

	/**
	 * Asigns a new var to the template, with the specified
	 * value
	 *
	 * @param string The item name
	 * @param mixed The Variable to add
	 */
	public function addVariable( $name, $var ) {
		$this->vars[$name] = $var;
	}

	/**
	 * Renders the template and creates the form
	 *
	 * @return string
	 */
	public function __toString() {
		$template = $this->template;
		$vars = $this->vars;
		$vars['start'] = $this->start;
		$vars['end'] = $this->end;
		$vars['html'] = $this->html;
		$vars['items'] = $this->items;
		$vars['errors'] = $this->errors;

		$html = Gecko_Template::renderTemplate( $template, $vars, true );
		return $html;
	}
}